﻿var searchNewsByDatePicker = function () {
    var flagDateTimePicker = false;
    var handler = undefined;
    var placeHolder = undefined;
    var text = undefined;
    
    function showLoading() {
        text = handler.text();
        handler.text('Loading...');
    }

    function removeLoading() {
        handler.text(text);
    }

    return {
        init: function (elmt, elmtPlaceHolder) {
            handler = elmt;
            handler.width(handler.width());
            placeHolder = elmtPlaceHolder;
            handler.unbind('click').bind('click',
                    function () {
                        if (flagDateTimePicker) {
                            placeHolder.datepicker('show');
                        } else {
                            showLoading($(this));
                            $.getScript('http://code.jquery.com/ui/1.10.2/jquery-ui.min.js', function () {
                                placeHolder.datepicker({
                                    dateFormat: 'dd-mm-yy',
                                    maxDate: new Date(),
                                    onSelect: function (selectedDate) {
                                        var url = String.format("/{0}/tin-theo-ngay/{1}.htm", $("#hidCatUrl").val(), selectedDate);
                                        window.location.href = url;
                                        return false;
                                    }
                                });
                                placeHolder.datepicker('show');
                                handler.unbind('click').bind('click', function () {
                                    placeHolder.datepicker('show');
                                });
                                flagDateTimePicker = true;
                                removeLoading();
                            });
                        }
                    });
        }
    };

} (jQuery);